package com.touch.client;
import static com.touch.client.databank.StringBank.CD;
import java.net.*;
import javax.swing.*;

import com.touch.client.databank.SpriteBank;
import com.touch.client.forms.*;
import com.touch.core.net.*;
import com.touch.core.util.XMLFile;
public class ClientApplication 
{
	public static void main(String args[]) throws Exception
	{		
		final String configLocation = CD + "config.xml";
		final XMLFile config = new XMLFile(new URL(configLocation).openStream());			
		boolean altIcon = false;
		final MainPanel frame = new MainPanel(false);
		System.out.println(CD);		
		SpriteBank.XML_SPRITES = config.readURL(CD, "config", "def", "sprites");
		
		frame.setTitle("Solar Eclipse");
		frame.setSize(640, 480);		
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		try
		{
			String iconLoc = config.readURL(CD, "config", "icon", "url");
			URL uIcon = new URL(iconLoc);
			frame.setIconImage(new ImageIcon(uIcon).getImage());
		}
		catch(Exception defaultIconExcepion)
		{
			System.out.println("Cannot find icon!");
			System.out.println("Attempting to use alternate icon...");
			altIcon = true;
		}
		loadAlternateIcon:
		{
			if(altIcon == false)
				break loadAlternateIcon;
			try
			{
				String iconLoc = config.readURL(CD, "config", "icon", "alt");
				URL uIcon = new URL(iconLoc);
				frame.setIconImage(new ImageIcon(uIcon).getImage());
			}
			catch(Exception defaultIconExcepion)
			{
				System.out.println("Cannot find alternate icon!");
				System.out.println("Continuing without using an icon...");				
			}
		}
			
		
		ClientControls.CLIENT_SOCKET = new UDPClient(new InetSocketAddress("::1", 62594));
		ClientControls.CLIENT_SOCKET.addNetListener(new ClientSocketEvents());
		new Thread(new ConnectionChecker()).start();
	}
}
